package com.bwie.orders.listen;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.bwie.orders.dao.OrderDao;
import com.bwie.orders.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author ShuangyeZhao
 * @date 2022/3/3
 */
@Component
public class MyOrderListener {

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private ThreadPoolExecutor executor;


    @RabbitListener(queues = "miaosha_order")
    public void save2(String odr, Message message, Channel channel) throws IOException {
        System.out.println(odr);
        OrderEntity orderEntity = JSON.parseObject(odr, new TypeReference<OrderEntity>() {
        });

//       CompletableFuture<Integer> cps=CompletableFuture.supplyAsync(()->{
//           return 0;
//        },executor);

        orderDao.insert(orderEntity);
        //手动确认 保证消息不会重复修复

        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

    }

}
